package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.advantagesup.common.PageBean;
import com.itheima.advantagesup.common.PaginationVO;
import com.itheima.advantagesup.entity.SysRole;
import com.itheima.advantagesup.entity.YddCate;
import com.itheima.advantagesup.mapper.ContentMapper;
import com.itheima.advantagesup.service.ContentService;
import com.itheima.advantagesup.vo.RoleVO;
import com.itheima.advantagesup.vo.cateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ContentServiceImpl implements ContentService {
    // 注入mapper
    private final ContentMapper contentMapper;

    public ContentServiceImpl(ContentMapper contentMapper) {
        this.contentMapper = contentMapper;
    }

    @Override
    public List<YddCate> getAllContent() {
        List<YddCate> cate = contentMapper.getAllContent();
        return cate;
    }

    @Override
    public PaginationVO<cateVO> getPageContent(String catename, String page, String limit) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<YddCate> cate = contentMapper.getPageContent(catename);
        PageInfo<YddCate> pageInfo = new PageInfo<>(cate);
        long count = pageInfo.getTotal();
        int totalPages = pageInfo.getPages();
        int pageSize = pageInfo.getPageSize();
        int currentPage = pageInfo.getNextPage();
        List<YddCate> yddCateList = pageInfo.getList();

        List<cateVO> cateList = new ArrayList<>();
        yddCateList.forEach(c -> cateList.add(c.getCateVO()));

        PaginationVO<cateVO> paginationVO = new PaginationVO<>();
        paginationVO.setCount(count);
        paginationVO.setTotalPages(totalPages);
        paginationVO.setPageSize(pageSize);
        paginationVO.setCurrentPage(currentPage);
        paginationVO.setData(cateList);
        return paginationVO;
    }

    @Override
    public void delContent(String id) {
        contentMapper.delContent(id);
    }

    @Override
    public void addContent(cateVO cv) {
        contentMapper.addContent(cv);
    }

    @Override
    public cateVO rowInfoContent(String id) {
        cateVO cv = contentMapper.rowInfoContent(id);
        return cv;
    }

    @Override
    public void updateContent(cateVO cv) {
        contentMapper.updateContent(cv);
    }
}
